let SynObj = require('../jce/SynchronousProxy').News;

let GetConDifInfo = require('../server/GetConfDifInfo');
let GetConDifSimpleInfo = require('../server/GetConfDifSimpleInfo');
let DbProxyObj = require('../jce/DBProxyProxy').Base;

let GetConDif = function () {


    this.rstConn = new DbProxyObj.DBConnInfo();
    this.tstConn = new DbProxyObj.DBConnInfo();

    this.rstConn.readFromObject({
        host: global.configs.mysqlDbInfo.realMysql.host,
        port: global.configs.mysqlDbInfo.realMysql.port,
        dbName: global.configs.mysqlDbInfo.realMysql.db_name,
        user: global.configs.mysqlDbInfo.realMysql.user,
        pass: global.configs.mysqlDbInfo.realMysql.password,
        charSet: global.configs.mysqlDbInfo.realMysql.charSet
    });

    this.tstConn.readFromObject({
        host: global.configs.mysqlDbInfo.testMysql.host,
        port: global.configs.mysqlDbInfo.testMysql.port,
        dbName: global.configs.mysqlDbInfo.testMysql.db_name,
        user: global.configs.mysqlDbInfo.testMysql.user,
        pass: global.configs.mysqlDbInfo.testMysql.password,
        charSet: global.configs.mysqlDbInfo.testMysql.charSet
    });
};


GetConDif.prototype.init = function(SynDifReq){
    let self = this;
    /** 获取站点表 和 规则表 测试库和正式库的 数据 */
    let stable = [];
    stable.push(global.configs.mysqlDbInfo.StationTable);
    stable.push(global.configs.mysqlDbInfo.RulesTable);

    let pList = [];
    for (let table of stable) {
        let sql = `SELECT * FROM \`${table}\`;`

        // pList.push(dbProxy.queryRecord(global.configs.mysqlDbInfo.realMysql,sql));
        // pList.push(dbProxy.queryRecord(global.configs.mysqlDbInfo.testMysql,sql));


        pList.push(global.tafPrx.dbProxy.queryRecord(self.rstConn, sql));
        // pList.push(global.tafPrx.dbProxy.queryRecord(self.tstConn, sql));
        pList.push(global.mysqlDb.testDb.query(sql))
    }
    /** plist...  stable     dbConInfo
     *           sta(0)      real(0)/test(1)
     *           rules(1)    real(2)/test(3)
     */
    return pList;
}

GetConDif.prototype.getConInfo = function (SynDifReq) {

    let pList = this.init(SynDifReq);
    /** 获取差异数据 */

    return Promise.all(pList)
        .then(res => {
            let DifInfo = new GetConDifInfo();
            let getRecordDIfRsp = new SynObj.GetRecordDifRsp();
            try {
                if (SynDifReq.difType == 1) {         //STATION_DIF
                    getRecordDIfRsp=DifInfo.difDbInfo(res[0], res[1], 1);
                } else if (SynDifReq.difType == 2) {   //RULE_DIF
                    getRecordDIfRsp=DifInfo.difDbInfo(res[2], res[3], 2);
                } else {                              //默认为ALL_DIF
                    let stationDifInfo = DifInfo.difDbInfo(res[0], res[1], 1);
                    let ruleDifInfo = DifInfo.difDbInfo(res[2], res[3], 2);
                    getRecordDIfRsp.vDifRecord.value = stationDifInfo.vDifRecord.value.concat(ruleDifInfo.vDifRecord.value);
                }
                getRecordDIfRsp.ret=0;
                getRecordDIfRsp.msg='success on get Db_difInfo';

            }catch (e) {
                getRecordDIfRsp.ret=1;
                getRecordDIfRsp.msg=e.message+'\n'+e.stack;
            }finally {
                return getRecordDIfRsp;
            }

        }).catch(err => {
            let a;
        })

}

GetConDif.prototype.getConSimpleInfo = function (SynDifReq) {
    let pList = this.init(SynDifReq);

    /** 获取差异数据 */
    return Promise.all(pList)
        .then(res => {
            let DifSimpleInfo = new GetConDifSimpleInfo()
            let getRecordDIfRsp = new SynObj.GetRecordDifRsp();
            try {
                if (SynDifReq.difType == 1) {         //STATION_DIF
                    getRecordDIfRsp=DifSimpleInfo.difDbSimpleInfo(res[0], res[1], 1);
                } else if (SynDifReq.difType == 2) {   //RULE_DIF
                    getRecordDIfRsp=DifSimpleInfo.difDbSimpleInfo(res[2], res[3], 2);
                } else {                              //默认为ALL_DIF
                    let stationDifInfo = DifSimpleInfo.difDbSimpleInfo(res[0], res[1], 1);
                    let ruleDifInfo = DifSimpleInfo.difDbSimpleInfo(res[2], res[3], 2);
                    getRecordDIfRsp.vDifRecord.value = stationDifInfo.vDifRecord.value.concat(ruleDifInfo.vDifRecord.value);
                }
                getRecordDIfRsp.ret=0;
                getRecordDIfRsp.msg='success on get Db_difSimpleInfo';

            }catch (e) {
                getRecordDIfRsp.ret=1;
                getRecordDIfRsp.msg=e.message+'\n'+e.stack;
            }finally {
                return getRecordDIfRsp;
            }

        }).catch(err => {
            let a;
        })

}

module.exports = GetConDif;